package sebjul.ecom.servlet;

import java.io.*;
import java.util.*;

import javax.ejb.EJB;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

import sebjul.ecom.dao.*;
import sebjul.ecom.entities.*;

@WebServlet(urlPatterns={"/m", "/acceuil"})
public class Index extends HttpServlet {
	
	@EJB
	ArticleDao articleDao;
	@EJB
	GenreDao genreDao;
	
	
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int nbrpage=2;
		int idpage = 1;
		String stidpage=request.getParameter("idpage");
		if (stidpage != null){idpage = Integer.parseInt(request.getParameter("idpage"));}
		int numpage = ((idpage-1)*(nbrpage)); 
		List<Article> articles = articleDao.findArticleByPage(numpage,nbrpage);
		request.setAttribute("articles", articles);
			int pagination= articleDao.countArticles();
			
			float nombrepage=pagination/nbrpage;
			float reste= pagination % nbrpage; 
			int pageplus=0;
			if (reste>0){pageplus=1;}
			pagination=((int) (nombrepage))+pageplus;
			
		request.setAttribute("origine", "acceuil?");
		request.setAttribute("idpage", idpage);
		request.setAttribute("pagination", pagination);
		RequestDispatcher rd = request.getRequestDispatcher("catalogue.jsp");
		rd.forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}